package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.RbacRole;
import com.woniu.entity.ResponseEntity;
import com.woniu.mapper.RbacManagerMapper;
import com.woniu.mapper.RbacRoleMapper;
import com.woniu.service.RbacRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author Rain
 * @Description :
 * @date 2022/09/02 16:00
 */
@Service
@Transactional
public class RbacRoleServiceImpl implements RbacRoleService {

    @Autowired
    RbacRoleMapper mapper;

    @Autowired
    RbacManagerMapper managerMapper;

    @Override
    public ResponseEntity findAll(int pageNum, int pageSize, RbacRole role) {
        PageHelper.startPage(pageNum, pageSize);

        PageInfo<RbacRole> pageInfo = new PageInfo<>(mapper.findAll(role));
        return new ResponseEntity(200, "查询成功", pageInfo);
    }

    @Override
    public ResponseEntity findOne(int id) {
        return null;
    }

    @Override
    public ResponseEntity addOne(RbacRole role) {
        int count = mapper.findCountByNameOrIdOrCode(role);
        if (count > 0)
            return new ResponseEntity(500, "添加失败,角色重复", null);
        else {
            int i = mapper.insertOne(role);
            return new ResponseEntity(200, "添加成功", null);
        }
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int i = mapper.deleteOne(id);
        if (i > 0)
            return new ResponseEntity(200, "删除成功", null);
        else
            return new ResponseEntity(500, "删除失败", null);
    }

    @Override
    public ResponseEntity updateOne(RbacRole role) {
        int count = mapper.findCountByNameOrIdOrCode(role);
        if (count > 1) {
            return new ResponseEntity(500, "修改失败,角色名或代码重复", null);
        } else {
            int i = mapper.updateOne(role);
            System.out.println(i);
            return new ResponseEntity(200, "修改角色成功", null);
        }
    }

    @Override
    public Set<String> findOneByName(String username) {

        return mapper.findOneByName(username);
    }

    @Override
    public ResponseEntity updateRoleStatus(Integer id, String status) {
        int count = mapper.updateRoleStatus(id, status);
        if (count > 0) {
            return new ResponseEntity(200, "修改状态成功", null);
        } else {
            return new ResponseEntity(500, "修改状态失败", null);
        }
    }

    @Override
    public ResponseEntity findCheckedID(Integer id) {
        List<Integer> menuIDByRoleID = mapper.findMenuIDByRoleID(id);

        return new ResponseEntity(200,null,menuIDByRoleID);
    }

    @Override
    public ResponseEntity removeMenuByRoleID(Integer roleID, Integer menuID) {
        mapper.removeMenuByRoleID(roleID, menuID);

        return new ResponseEntity(200,"修改成功",null);
    }

    @Override
    public ResponseEntity addMenuByRoleID(Integer roleID, Integer menuID) {
        mapper.addMenuByRoleID(roleID, menuID);
        return new ResponseEntity(200,"修改成功",null);
    }

    @Override
    public ResponseEntity roleMenuSave(Integer roleID, List<Integer> menuIDs) {
        mapper.deleteAllMenuByRoleID(roleID);
        for (Integer menuID : menuIDs) {
            mapper.addMenuByRoleID(roleID, menuID);
        }

        return new ResponseEntity(200,"保存成功",null);
    }
}
